﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.Structure.Finance.Strategies.SpikeArb;
using Algobox.Structure.Finance.Strategies.Terrapin;
using Algobox.Objects.Finance;
using System.Collections.Concurrent;
using Algobox.Objects.Finance.Assets.Stock;
using Algobox.Structure.Finance.Strategies.Moss;
using Algobox.Structure.Finance.Strategies.Dastan;
using Algobox.Structure.Finance.Strategies.FractionPrototype;

namespace Algobox.Data.Finance
{
    public class SterlingTestData : ITerrapinData, IMossData, IDastanData, IFractionData
    {
        public SterlingTestData(ConcurrentDictionary<string, StockSecurity> stocks)
        {
            _stocks = stocks;
            LoadStocks();
        }

        ConcurrentDictionary<string, StockSecurity> _stocks;
        public IDictionary<string, StockSecurity> Stocks { get { return _stocks; } }

        public string FeedName
        {
            get { return "SterlingTerrapinTestData"; }
        }
        
        public bool TryLoadStrategyTerrapin(TerrapinManagement manager)
        {
            StockSecurity security;
            string account = "LQA001";
            if (_stocks.TryGetValue("ENRC.LN", out security))
            {
                manager.AddOrUpdate(Guid.NewGuid(), security, account, 1000, 2, 2, 2, 2);
            }

            return true;
        }

        public bool TrySaveStrategyTerrapin(TerrapinManagement manager)
        {
            return true;
        }

        private void LoadStocks()
        {
            AddStock("LLOY.LN");
            AddStock("CPW.LN");
            AddStock("HSBA.LN");
            AddStock("BP.LN");
            AddStock("WOS.LN");
            AddStock("ANTO.LN");
            AddStock("VOD.LN");
            AddStock("ARM.LN");
            AddStock("CNE.LN");
            AddStock("TLW.LN");
            AddStock("OML.LN");
            AddStock("WEIR.LN");
            AddStock("KAZ.LN");
            AddStock("ISYS.LN");
            AddStock("ENRC.LN");
            AddStock("PSN.LN");
            AddStock("AV.LN");
            AddStock("ARM.LN");
            AddStock("EMG.LN");  
        }

        public StockSecurity AddStock(string symbol)
        {
            return _stocks.AddOrUpdate(symbol,
                (key) =>
                {
                    StockSecurity security = new StockSecurity(key, Markets.MarketMIC.XLON)
                    {
                        Currency = Currencies.Currency.GBX,
                        Index = "FTSE100",
                        Sector = "SectorB",
                        IndustryGroup = "IndustryB",
                        Industry = "IndustryGroupB1",
                        ISIN = "GB0000000000",
                        Phase = Phases.Phase.Open
                    };
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.XLON, new StockListing(security, Exchanges.ExchangeMIC.XLON, Algobox.Objects.Finance.Assets.TickStatic.Default(), 3));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.BATE, new StockListing(security, Exchanges.ExchangeMIC.BATE, Algobox.Objects.Finance.Assets.TickStatic.Default(), 1));
                    security.Listings.TryAdd(Exchanges.ExchangeMIC.CHIX, new StockListing(security, Exchanges.ExchangeMIC.CHIX, Algobox.Objects.Finance.Assets.TickStatic.Default(), 2));
                    return security;
                },
                (key, security) =>
                {
                    return security;
                });
        }



        public bool TryLoadStrategyMoss(string username, MossManagement manager)
        {
            StockSecurity security;
            if (_stocks.TryGetValue("VOD.LN", out security))
            {
                manager.AddOrUpdate(Guid.NewGuid(), security, "LQA001", 1000, 70, 30, 5, 14, 1.5);
            }

            return true;
        }

        public bool TrySaveStrategyMoss(string username, MossManagement manager)
        {
            return true;
        }


        public bool TryLoadStrategyDastan(DastanManagement manager)
        {
            StockSecurity security;
            string account = "LQA001";
            if (_stocks.TryGetValue("ENRC.LN", out security))
            {
                manager.AddOrUpdate(Guid.NewGuid(), security, account, 1000, 2, 2, 2, 2);
            }
            return true;
        }

        public bool TrySaveStrategyDastan(DastanManagement manager)
        {
            return true;
        }


        public bool TryLoadStrategyFraction(string username, FractionManagement manager)
        {
            throw new NotImplementedException();
        }

        public bool TrySaveStrategyFraction(string username, FractionManagement manager)
        {
            throw new NotImplementedException();
        }
    }
}
